package com.cnzz.site1255174697.activity.launcher;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import com.alibaba.baichuan.android.trade.AlibcTradeSDK;
import com.alibaba.baichuan.android.trade.callback.AlibcTradeInitCallback;
import com.alibaba.baichuan.android.trade.model.AlibcTaokeParams;
import com.cnzz.site1255174697.activity.MainActivity;
import com.cqyanyu.yanyu.activity.XActivity;

import java.util.Timer;
import java.util.TimerTask;

/**
 * 启动页
 * Created by cheng on 2016/8/18.
 */
//@ContentView(R.layout.activity_start)
public class StartActivity extends XActivity {
    private Timer timer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        execute();
        AlibcTradeSDK.asyncInit(StartActivity.this, new AlibcTradeInitCallback() {
            @Override
            public void onSuccess() {
                Log.d("Application", "初始化成功");
                //初始化成功，设置相关的全局配置参数
                AlibcTaokeParams alibcTaokeParams = new AlibcTaokeParams("mm_112803358_0_0", null, null);
                AlibcTradeSDK.setTaokeParams(alibcTaokeParams);
                AlibcTradeSDK.setSyncForTaoke(true);
                AlibcTradeSDK.setForceH5(true);
                if (isInitEnd) {
                    startActivity();
                } else {
                    isInitEnd = true;
                }
            }
            @Override
            public void onFailure(int code, String msg) {
                Log.d("Application", "初始化失败" + code + "  " + msg);
                //初始化失败，可以根据code和msg判断失败原因，详情参见错误说明
            }
        });
    }

    /**
     * 跳转
     */
    private void startActivity() {
//        if (XAppUtil.isFirstEnter(this, getPackageName())) {
////            startActivity(new Intent(this, FirstStartActivity.class));
//        } else {
//            if (UserInfoFactory.getUserInfo().uid > 0) {
        startActivity(new Intent(this, MainActivity.class));
//            } else {
////                startActivity(new Intent(this, LoginActivity.class));
//            }
//        }
        finish();
    }

    private boolean isInitEnd = false;

    /**
     * 执行计时
     */
    private void execute() {
        final long time = 1000;
//        if (startInfo != null) {
//            time = startInfo.getStart_img_time();
//        }
        timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (AlibcTradeSDK.isInitialized()) {
                            startActivity();
                        } else if (isInitEnd) {
                            startActivity();
                        } else {
                            isInitEnd = true;
                        }
                    }
                });
            }
        }, time); //延时1000ms后执行，1000ms执行一次

    }
}
